import React, { RefObject } from 'react'
import {
  Form,
  Input,
  Button,
  TextArea,
  Stepper,
  Switch,
} from 'antd-mobile'
import request from '../../api/request'
import JSEncrypt from 'jsencrypt';

function Index() {

  const publicKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB`;

  const encryptor = new JSEncrypt();
  encryptor.setPublicKey(publicKey); // 设置公钥

  const encrypt = (text) => {
    return encryptor.encrypt(text);
    // return encryptor.encrypt(text).toString(); // 返回字符串格式的密文
  };

   const onFinish = (values) => {
    values.pwd = encrypt(values.pwd)

    request.post('/user/v1/login',{
      ...values
    }).then(res=>{
      console.log(res)
      localStorage.setItem('headPic',res.data.result.headPic)//头像
      localStorage.setItem('userId',res.data.result.userId)//用户ID
      localStorage.setItem('userName',res.data.result.userName)//用户名
      localStorage.setItem('nickName',res.data.result.nickName)//昵称
      localStorage.setItem('sessionId',res.data.result.sessionId)//会话ID
      window.location.href = '/'
    })

  }
  return (
    <div>
      <Form
        layout='horizontal'
        onFinish={onFinish}
        footer={
          <Button block type='submit' color='primary' size='large'>
            登录
          </Button>
        }
      >
        <Form.Item
          name='email'
          label='账号'
          rules={[{ required: true, message: '姓名不能为空' }]}
        >
          <Input onChange={console.log} placeholder='请输入姓名' />
        </Form.Item>
        <Form.Item label='密码' name='pwd'>
            <Input placeholder='请输入密码' clearable type='password' />
          </Form.Item>
      </Form>
    </div>
  )
}

export default Index
